//
//  NetWorkManager.swift
//  XXIOT
//
//  Created by xx on 2024/8/21.
//

import UIKit
import FastSwiftSDK

class NetWorkManager: NSObject {
    public static let shared = NetWorkManager()
    let net = FastNetWorking()
    //    let baseurl = "http://www.iot.8bt.top:8082"
        let baseurl = "http://8.137.99.64:8082"
//    let baseurl = "http://192.168.9.64:8082"
    
    override init() {
        net.encoding = .JSONEncoding
        net.headers = [
            "Accept": "application/json",
            "userId": userId
        ]
        FastNetWorking.shared.encoding = .JSONEncoding
    }
    // 获取设备列表
    func getDeviceList(data:Dictionary<String,Any>,suc:@escaping FastDicBlock,fai:@escaping FastStrBlock){
        let url = "\(baseurl)/getDeviceList"
        net.post(url, data) { dic in
            if dic.int("code") == 1{
                if let data = dic.value(Dictionary<String,Any>.self, "data"){
                    suc(data)
                }
            }else{
                fai(dic.stringConversion("msg"))
            }
        } fai: { str in
            fai(str)
        }
    }
    // 添加设备
    func addDevice(data:Dictionary<String,Any>,suc:@escaping FastStrBlock,fai:@escaping FastStrBlock){
        let url = "\(baseurl)/addDevice"
        net.post(url, data) { dic in
            if dic.int("code") == 1{
                suc("添加成功")
            }else{
                fai(dic.stringConversion("msg"))
            }
        } fai: { str in
            fai(str)
        }
    }
    // 删除设备
    func deleteDevice(mac:String,suc:@escaping FastStrBlock,fai:@escaping FastStrBlock){
        let url = "\(baseurl)/deleteDevice"
        net.post(url, ["mac":mac]) { dic in
            if dic.int("code") == 1{
                suc("删除成功")
            }else{
                fai(dic.stringConversion("msg"))
            }
        } fai: { str in
            fai(str)
        }
    }
    
    // 绑定
    func binding(data:Dictionary<String,Any>,suc:@escaping FastStrBlock,fai:@escaping FastStrBlock){
        let url = "\(baseurl)/addDevice"
        net.post(url, data) { dic in
            if dic.int("code") == 1{
                suc("绑定成功")
            }else{
                fai(dic.stringConversion("msg"))
            }
        } fai: { str in
            fai(str)
        }
    }
    // 解绑
    func unbind(data:Dictionary<String,Any>,suc:@escaping FastStrBlock,fai:@escaping FastStrBlock){
        let url = "\(baseurl)/addDevice"
        net.post(url, data) { dic in
            if dic.int("code") == 1{
                suc("绑定失败")
            }else{
                fai(dic.stringConversion("msg"))
            }
        } fai: { str in
            fai(str)
        }
    }
    // 获取任务列表
    func getTaskList(){
        
    }
    // 删除任务
    func deleteTask(){
        
    }
    // 添加任务
    func addTask(){
        
    }
    
}
